package com.zt.ztyh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zt.ztyh.dto.LoanDto;
import com.zt.ztyh.entity.Loan;
import com.zt.ztyh.service.LoanService;

import com.zt.ztyh.vo.common.Result;
import com.zt.ztyh.vo.common.ResultCodeEnum;
import com.zt.ztyh.vo.loan.LoanVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/user/function/loan")
@Tag(name = "贷款模块")
public class LoanController {

    @Autowired
    private LoanService loanService;

    /***
     * 分页查询贷款信息
     * 流程：查看贷款信息,根据贷款信息返回去
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/findLoans/{page}/{limit}")
    @Operation(summary = "分页查询贷款信息")
    public Result<Page<Loan>> findLoans(@PathVariable("page") Integer page,
                                        @PathVariable("limit") Integer limit
    ,
                                       LoanDto loanDto ) {
        return Result.build(loanService.findLoans(page, limit,loanDto), ResultCodeEnum.SUCCESS);
    }

    /***
     * 根据id查询贷款信息
     * @param
     * @return
     */
    @GetMapping("/findLoan/{page}/{limit}")
    @Operation(summary = "根据自己的贷款信息")
    public Result<LoanVo> findLoan(@PathVariable("page") Integer page,
                                   @PathVariable("limit") Integer limit,
                                   LoanDto loanDto) {
        return Result.build(loanService.findLoan(page,limit,loanDto), ResultCodeEnum.SUCCESS);
    }

    /**
     * 添加贷款信息
     * @param loanDto
     * @return
     */
    @PostMapping("/selectUserLoan")
    @Operation(summary = "查询用户最高额度")
    public Result<BigDecimal> insertLoan(@RequestBody LoanDto loanDto){
        BigDecimal bigDecimal = loanService.selectUserLoan(loanDto);
        return Result.build(bigDecimal, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/addLoan")
    @Operation(summary = "添加贷款信息")
    public Result addLoan(@RequestHeader(name = "token")String token,@RequestBody LoanDto loanDto){
        loanService.addLoan(token,loanDto);
        return Result.build(null, 200,"成功");
    }

    @PostMapping("/updateLoan")
    @Operation(summary = "还款信息")
    public Result updateLoan(@RequestHeader(name = "token") String token,@RequestBody LoanDto loanDto){
        loanService.updateLoan(token,loanDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
